import os
import uuid
from fastapi import APIRouter, Form,  Depends, UploadFile, File
from sqlalchemy.orm import Session
import db
from models import HarpAudio

router = APIRouter()
storage_path = os.path.join(os.getcwd(), 'storage/')
if not os.path.exists(storage_path):
    os.makedirs(storage_path)

@router.post("/addaudio")
async def register_with_audio(audio_url: UploadFile = File(None)):
    unique_id = uuid.uuid4()
    if audio_url:
        file_extension = os.path.splitext(audio_url.filename)[1]
        unique_filename = f"{unique_id}{file_extension}"
        file_path = os.path.join(storage_path, unique_filename)
        with open(file_path, "wb") as buffer:
            buffer.write(await audio_url.read())
        audio_url = f"storage/{unique_filename}"
    else:
        audio_url = None
    return {"audio_url": audio_url}